package com.example.maggie.meizinote.mvp.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.maggie.meizinote.app.bean.MyUser;
import com.example.maggie.meizinote.mvp.ui.widget.YesDialog;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.example.maggie.meizinote.di.component.DaggerRegisterComponent;
import com.example.maggie.meizinote.di.module.RegisterModule;
import com.example.maggie.meizinote.mvp.contract.RegisterContract;
import com.example.maggie.meizinote.mvp.presenter.RegisterPresenter;

import com.example.maggie.meizinote.R;


import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View {
    @BindView(R.id.cet_email)
    EditText cet_email;
    @BindView(R.id.et_pwd)
    EditText et_pwd;
    @BindView(R.id.et_repwd)
    EditText et_repwd;
    @BindView(R.id.btn_login)
    Button btn_login;
    @BindView(R.id.password)
    LinearLayout password;
    private boolean isRegiste;

    @OnClick(R.id.btn_login)
    void onRegister() {
        if (isRegiste)
            mPresenter.onRegister(cet_email.getText().toString(), et_pwd.getText().toString(), et_repwd.getText().toString());
        else mPresenter.onRechangePassword(cet_email.getText().toString());
    }

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerRegisterComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .registerModule(new RegisterModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_register; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        isRegiste = getIntent().getBooleanExtra("isRegiste", true);
        if (!isRegiste) {
            password.setVisibility(View.GONE);
            setTitle("重置密码");
            btn_login.setText("提交申请");
        } else
            setTitle("注册");
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    public void result(String email, String pwd) {
        Intent intent = new Intent();
        intent.putExtra("username", email);
        intent.putExtra("password", pwd);
        setResult(911, intent);
        killMyself();
    }

    @Override
    public void showDialog(String content, boolean isClose) {
        new YesDialog.Builder(this)
                .setTitle("提示")
                .setMessage(content)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (isClose) killMyself();
                    }
                }).create().show();
    }
}
